﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eShop.DataAccess.Converter
{
    public class StateConverter
    {
        public static void AttachAssociation(List<Model.MetaData.State> models, List<Model.MetaData.Country> associations)
        {
            foreach (Model.MetaData.State model in models)
            {
                model.StateCountry = associations.Single(c => c.Id == model.StateCountry.Id);
                model.StateCountry.States.Add(model);
            }
        }

        public static List<Model.MetaData.State> ConvertToModelObject(List<Entity.State> entities)
        {
            List<Model.MetaData.State> models = new List<Model.MetaData.State>(entities.Count);
            foreach (Entity.State entity in entities)
            {
                models.Add(ConvertToModelObject(entity));
            }
            return models;
        }

        public static Model.MetaData.State ConvertToModelObject(Entity.State entity)
        {
            if (entity == null) return null;
            Model.MetaData.State model = new Model.MetaData.State();
            model.Id = entity.Id;
            model.Name = entity.Name;
            model.StateCountry = new Model.MetaData.Country() { Id = entity.CountryId };
            return model;
        }
    }
}
